#coding:utf-8
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.

from django.conf import settings
import MySQLdb
import json
from aliexpress.api import datelist
import datetime,time
import random
import urllib
import requests

import hashlib

from API import *

from aliexpress.models import Country,TranslateContent,ProductDeclare,StoreInfo,\
    OrderMemo,OrderDealMark,DeliveryInformation,ChooseLogistics,OrderDeal
from aliexpress.views import countryTanslate

from django.views.decorators.csrf import csrf_exempt

from django.conf import settings

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )


host = settings.DATABASES['erp']['HOST']
user = settings.DATABASES['erp']['USER']
passwd = settings.DATABASES['erp']['PASSWORD']
db = settings.DATABASES['erp']['NAME']
port = int(settings.DATABASES['erp']['PORT'])


def linkDB():
    conn=MySQLdb.connect(host=host,user=user,passwd=passwd,db=db,port=port,charset='utf8')
    #conn=MySQLdb.connect(host='120.76.134.53',user='remote_user',passwd='tradetrends',db='erp',port=3306,charset='utf8')
    return conn


def custom_sql(sql):
    from django.db import connection, transaction
    cursor = connection.cursor()

    # 数据修改操作——提交要求
    #cursor.execute(sql)
    #transaction.commit_unless_managed()

    # 数据检索操作,不需要提交
    cursor.execute(sql)
    rawData = cursor.fetchall()
    col_names = [desc[0] for desc in cursor.description]

    result = []
    for row in rawData:
        objDict = {}
        # 把每一行的数据遍历出来放到Dict中
        for index, value in enumerate(row):
            objDict[col_names[index]] = str(value)

        result.append(objDict)

    return result


def my_update_sql(sql):
    conn = linkDB()
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()

def my_custom_sql(sql):
    #conn=MySQLdb.connect(host='120.76.134.53',user='remote_user',passwd='tradetrends',db='t_crm',port=3306,charset='utf8')
    conn = linkDB()
    cursor = conn.cursor()

    # 数据修改操作——提交要求
    #cursor.execute(sql)
    #transaction.commit_unless_managed()

    # 数据检索操作,不需要提交
    cursor.execute(sql)
    rawData = cursor.fetchall()
    col_names = [desc[0] for desc in cursor.description]

    result = []
    for row in rawData:
        objDict = {}
        # 把每一行的数据遍历出来放到Dict中
        for index, value in enumerate(row):
            objDict[col_names[index]] = str(value)

        result.append(objDict)

    return result